<?php

// +----------------------------------------------------------------------
// | sitemap
// +----------------------------------------------------------------------

class sitemap_controller extends base_controller {

	protected $image_num = 2000;
	protected $keywords_num = 2000;

	public function _init() {
		$dir = dirname(APP_PATH) . '/sitemap/';
		if (!is_dir($dir)) {
			mk_dir($dir);
		}
	}

	//sitemap全站地图生成
	public function run(){
		$this->index();
		$this->category();
		$this->image();
		$this->keyword();
	}


	//site map索引页
	public function index() {
		$file = dirname(APP_PATH) . '/sitemap.xml';
		$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
		$xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
		$xml .= '<sitemap>' . "\n" .
				'<loc>' . $this->url['www'] . '/sitemap/categories.xml</loc>' . "\n" .
				'<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n" .
				'</sitemap>' . "\n";

		//图片索引页
		$total = M('image')->count();
		$perPage = $this->image_num;
		$pages = ceil($total / $perPage);
		for ($i = 1; $i <= $pages; $i++) {
			if ($i > 1) {
				$xml.='<sitemap>' . "\n";
				$xml.='<loc>' . $this->url['www'] . '/sitemap/wallpaper-' . $i . '.xml</loc>' . "\n";
				$xml.='<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml.='</sitemap>' . "\n";
			} else {
				$xml.='<sitemap>' . "\n";
				$xml.='<loc>' . $this->url['www'] . '/sitemap/wallpaper.xml</loc>' . "\n";
				$xml.='<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml.='</sitemap>' . "\n";
			}
		}

		//关键词索引页
		$total = M('keywords')->count();
		$perPage = $this->keywords_num;
		$pages = ceil($total / $perPage);
		for ($i = 1; $i <= $pages; $i++) {
			if ($i > 1) {
				$xml.='<sitemap>' . "\n";
				$xml.='<loc>' . $this->url['www'] . '/sitemap/keywords-' . $i . '.xml</loc>' . "\n";
				$xml.='<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml.='</sitemap>' . "\n";
			} else {
				$xml.='<sitemap>' . "\n";
				$xml.='<loc>' . $this->url['www'] . '/sitemap/keywords.xml</loc>' . "\n";
				$xml.='<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml.='</sitemap>' . "\n";
			}
		}
		$xml.='</sitemapindex>';
		file_put_contents($file, $xml);
		echo $file . "\n";
	}

	//生成分类页sitemap页面
	public function category() {
		$file = dirname(APP_PATH) . '/sitemap/categories.xml';

		$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . "\n";

		$soft = M('image');
		$cat = M('category');

		@unlink($file);
		$handle = fopen($file, 'a+');
		fwrite($handle, $xml);

		$cates = $cat->getChildTree(0);

		//先写入大分类
		foreach ($cates as $v) {
			$xml = '';
			$xml .= '<url>' . "\n";
			$xml .= '<loc>' . $this->url['www'] . $v['url'] . '</loc>' . "\n";
			$xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
			$xml .= '<changefreq>daily</changefreq>' . "\n";
			$xml .= '</url>' . "\n";
			fwrite($handle, $xml);
		}

		//开始写入小分类
		foreach ($cates as $v) {
			$sub_cat = $v['child'];
			$xml = '';
			foreach ($sub_cat as $sv) {
				$xml .= '<url>' . "\n";
				$xml .= '<loc>' . $this->url['www'] . $sv['url'] . '</loc>' . "\n";
				$xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml .= '<changefreq>daily</changefreq>' . "\n";
				$xml .= '</url>' . "\n";
			}
			fwrite($handle, $xml);
		}
		fwrite($handle, '</urlset>' . "\n");
		fclose($handle);
		chmod($file, 0777);
		echo $file . "\n";
	}

	//生成图片详细页列表
	public function image() {
		$perPage = $this->image_num; //每次取2K

		$total = $this->image->count();
		$pages = ceil($total / $perPage);
		
		$field = 'id,name,catid1,catid2,img';		
		$processed = 0;
		for ($page = 1; $page <= $pages; $page++) {
			if ($page == 1) {
				$filename = '/sitemap/wallpaper.xml';
			} else {
				$filename = '/sitemap/wallpaper-' . $page . '.xml';
			}
			$file = dirname(APP_PATH) . $filename;

			@unlink($file);
			$handle = fopen($file, 'a+');
			$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
			$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . "\n";
			fwrite($handle, $xml);
			$rows = $this->image->field($field)->limit($processed . ',' . $perPage)->findAll();
			$rows = $this->image->format($rows);

			$processed += $perPage;
			foreach ($rows as $k => $v) {
				$xml = '';
				$xml .= '<url>'."\n";
				$xml .= '<loc>' . $v['view_url'] . '</loc>'."\n";
				$xml .= '<image:image>'."\n";
				$xml .= '<image:loc>' . $v['img_480_360'] . '</image:loc>'."\n";
				$xml .= '</image:image>'."\n";
				$xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>'."\n";
				$xml .= '<changefreq>daily</changefreq>'."\n";
				$xml .= '</url>';
				fwrite($handle, $xml);
			}
			fwrite($handle, '</urlset>' . "\n");
			fclose($handle);
			chmod($file, 0777);
			echo $filename . "\n";
		}
	}

	//关键词的sitemap
	public function keyword() {
		$db = M('keywords');
		$total = $db->count();
		$perPage = $this->keywords_num;
		$pages = ceil($total / $perPage);
		if (!is_dir(dirname(APP_PATH) . '/sitemap'))
			mk_dir(dirname(APP_PATH) . '/sitemap');

		//如果数据过多就分页
		$processed = 0;
		for ($page = 1; $page <= $pages; $page++) {
			if ($page == 1) {
				$filename = '/sitemap/keywords.xml';
			} else {
				$filename = '/sitemap/keywords-' . $page . '.xml';
			}
			$file = dirname(APP_PATH) . $filename;
			@unlink($file);
			$handle = fopen($file, 'a+');
			$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
			$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . "\n";
			fwrite($handle, $xml);
			$keywords = $db->limit($processed . ',' . $perPage)->findAll();
			$processed += $perPage;
			foreach ($keywords as $k => $v) {
				$xml = '';
				$xml .= "<url>\n";
				$url = url('keyword', $v['name']);
				$xml .= '<loc>' . $url . '</loc>' . "\n";
				$xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
				$xml .= '<changefreq>daily</changefreq>' . "\n";
				$xml .= '</url>' . "\n";
				fwrite($handle, $xml);
			}
			fwrite($handle, '</urlset>' . "\n");
			fclose($handle);
			chmod($file, 0777);
			echo $filename . "\n";
		}
	}

}